<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入会员手机号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="IP地址" prop="ipaddr">
        <el-input
          v-model="queryParams.ipaddr"
          placeholder="请输入登录IP地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="登录地点" prop="loginLocation">
        <el-input
          v-model="queryParams.loginLocation"
          placeholder="请输入登录地点"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="浏览器" prop="browser">
        <el-input
          v-model="queryParams.browser"
          placeholder="请输入浏览器类型"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作系统" prop="os">
        <el-input
          v-model="queryParams.os"
          placeholder="请输入操作系统"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="登陆时间" style="width: 308px">
        <el-date-picker
          v-model="daterangeLoginTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          :default-time="[new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 1, 1, 23, 59, 59)]"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['ums:memberLogininfor:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="memberLogininforList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" v-if="true"/>
      <el-table-column label="会员手机号" align="center" prop="phone" />
      <el-table-column label="会员id" align="center" prop="memberId" />
      <el-table-column label="登录IP地址" align="center" prop="ipaddr" />
      <el-table-column label="登录地点" align="center" prop="loginLocation" />
      <el-table-column label="浏览器类型" align="center" prop="browser" />
      <el-table-column label="操作系统" align="center" prop="os" />
      <el-table-column label="登陆时间" align="center" prop="loginTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.loginTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script setup name="MemberLogininfor">
import { listMemberLogininfor, getMemberLogininfor, delMemberLogininfor, addMemberLogininfor, updateMemberLogininfor } from "@/api/ums/memberLogininfor";

const { proxy } = getCurrentInstance();

const memberLogininforList = ref([]);
const open = ref(false);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeLoginTime = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    phone: undefined,
    memberId: undefined,
    ipaddr: undefined,
    loginLocation: undefined,
    browser: undefined,
    os: undefined,
    loginTime: undefined
  },
  rules: {
    id: [
      { required: true, message: "ID不能为空", trigger: "blur" }
    ],
    phone: [
      { required: true, message: "会员手机号不能为空", trigger: "blur" }
    ],
    memberId: [
      { required: true, message: "会员id不能为空", trigger: "blur" }
    ],
    ipaddr: [
      { required: true, message: "登录IP地址不能为空", trigger: "blur" }
    ],
    loginLocation: [
      { required: true, message: "登录地点不能为空", trigger: "blur" }
    ],
    browser: [
      { required: true, message: "浏览器类型不能为空", trigger: "blur" }
    ],
    os: [
      { required: true, message: "操作系统不能为空", trigger: "blur" }
    ],
    loginTime: [
      { required: true, message: "登陆时间不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询会员登录记录列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeLoginTime && '' != daterangeLoginTime) {
    queryParams.value.params["beginLoginTime"] = daterangeLoginTime.value[0];
    queryParams.value.params["endLoginTime"] = daterangeLoginTime.value[1];
  }
  listMemberLogininfor(queryParams.value).then(response => {
    memberLogininforList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: undefined,
    phone: undefined,
    memberId: undefined,
    ipaddr: undefined,
    loginLocation: undefined,
    browser: undefined,
    os: undefined,
    loginTime: undefined
  };
  proxy.resetForm("memberLogininforRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeLoginTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加会员登录记录";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  loading.value = true
  reset();
  const _id = row.id || ids.value
  getMemberLogininfor(_id).then(response => {
    loading.value = false;
    form.value = response.data;
    open.value = true;
    title.value = "修改会员登录记录";
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('ums/memberLogininfor/export', {
    ...queryParams.value
  }, `memberLogininfor_${new Date().getTime()}.xlsx`)
}

getList();
</script>
